import ctime from '@/components/changeTime.vue'
import formalitles from '@/components/carDetail/formalities.vue'
import carDetail from '@/components/carDetail/carDetail.vue'
export default {
  components: {
    ctime,
    formalitles,
    carDetail
  },
  data() {
    return {
      cardetailList: [], //车辆详情
      companyName: '', //公司名称
      storeId: '', //公司id
      xedColorList: [], //选择后的外观颜色list
      xzwgcolor: {
        outerimg: {},
        inimg: {}
      }, //选择外观颜色
      xuanzhong: false, //选中颜色外观
      colortype: true, //未选择颜色,出现的问题提示
      //生产时间
      showsc: false,
      Production: '',
      modesc: 'time',
      defaultTimesc: new Date().toISOString().slice(0, 10),
      paramssc: {
        year: true,
        month: true,
        day: true,
        hour: false,
        minute: false,
        second: false
      },

      showxq: false, //车辆详情
      xqtitlet: '', //车辆详情title
      cardetail: null, //车辆详情
      showsx: false, //手续
      sxtitlet: '', //手续
      sxtitletid: null, //手续id
      showdz: false, //车辆所在地
      paramsdz: {
        province: true,
        city: true,
      },
      caradddresprovince: '', //地址参数
      caradddrescity: '', //地址参数

      showzt: false, //车辆状态
      zttitlet: '', //状态title

      showqy: false, //销售区域
      paramsqy: {
        province: true,
        city: true,
      },
      show: false,
      content: '您已成功发布新车！点击 ',
      contentxq: '查看详情>>',
      goodsidcar: '', //返回车子id
      remark: '', //备注
      specifAll: {
        Speciflist: {}, //规格
        brandlist: {}, //品牌
        Serieslist: {}, //系列
        Configulist: {}, //车子

      },
      // 上牌地址
      goodsRegion: '',
      goodsRegionName: '',
      // 区域要求
      regionLimitId: '',
      regionLimitName: '',

      Whethertosubmit: '1', //是否提交--编辑
      editId: '', //编辑寻车id
      specificationList: [],
      getOffShelf: false, //下架弹框
    }
  },

  onLoad(busslist) {
    // console.log(this.$store.state.userInfoDetail.store.storeName)
    // this.getCarDetail()
    this.companyName = this.$store.state.userInfoDetail.store.storeName
    this.storeId = this.$store.state.userInfoDetail.store.storeId
    this.nowCity = this.$store.state.city
    // 选择规格，品牌，系列，车子，地址
    uni.$on('specifAll', data => {
      Object.assign(this.specifAll, data)
      // console.log('品牌', this.specifAll)
    })
    // 选择后的图片
    uni.$on('carSourceColor', data => {
      Object.assign(this.xedColorList, data)
      this.xedColorList = data.colors
    })
    uni.$on('selectCity', res => {
      //1 上牌 2区域需求
      if (res.type === '1') {
        this.goodsRegion = res.value.id
        this.goodsRegionName = res.value.name

      } else {
        this.regionLimitId = res.value.id
        this.regionLimitName = res.value.name
      }
    })
    //1:发布寻车   2：编辑删除寻车
    console.log(busslist)
    this.Whethertosubmit = busslist.type
    if (busslist.type === '1') {
      uni.setNavigationBarTitle({
        title: '发布寻车'
      })
    } else if (busslist.type === '2') {
      this.editId = busslist.id
      this.getSpeciflist()
      this.setEdit()
      uni.setNavigationBarTitle({
        title: '编辑寻车'
      })
    }
  },
  methods: {

    //规格-赋值3
    specification() {
      //跳转规格
      uni.navigateTo({
        url: '/pages_common/chooseSpecifications/chooseSpecifications?type=110'
      })

      //重新点击规格,清空下面参数
      this.xedColorList = []
      this.colortype = true
    },
    //打开颜色弹框
    xzcolorlick() {
      if (!this.specifAll.Configulist) {
        setTimeout(function() {
          uni.showToast({
            title: '请先选择配置',
            duration: 2000,
            icon: 'none'
          })
        }, 100)
        return false
      } else {
        // 颜色列表
        uni.navigateTo({
          url: '/pages_common/exteriorInterior/exteriorInterior?colorList=' + encodeURIComponent(JSON.stringify(
            this
              .specifAll.Configulist)) + '&type=1110' + '&xedColorLists=' + encodeURIComponent(JSON.stringify(this.xedColorList))
        })
      }
    },

    // //删除颜色
    tagClick(e, index) {
      this.xedColorList.splice(index, 1) //取消
      if (this.xedColorList === '' || this.xedColorList === undefined || this.xedColorList.length === 0) {
        this.colortype = true
      }
    },

    //上牌地点
    Listingaddress(type) {
      // type:1---上牌地址 2----区域要求
      uni.navigateTo({
        url: '/pages_findCar/thereGistrationAddress/thereGistrationAddress?type=' + type + '&city=' + this.nowCity
      })
    },

    //生产时间
    confirmsc(e) {
      console.log(e)
      this.Production = ''
      this.showsc = false
      this.Production = e.year + '-' + e.month + '-' + e.day
      // ios 不支持yyyy/mm/dd 格式
      // 当前时间
      var myDate = new Date()
      // let year=myDate.getFullYear()
      // let month=(myDate.getMonth()+1)<10?'0'+(myDate.getMonth()+1):myDate.getMonth()+1
      // let day=myDate.getDate()<10?'0'+(myDate.getDate):myDate.getDate
      // let today=year + '-' + month + '-' + day
      // console.log(new Date(this.Production)>myDate)
      if (new Date(this.Production) > myDate) {} else {
        // console.log(this.Production,today)
        uni.showToast({
          title: '寻车有效时间不能低于当前时间',
          duration: 2000,
          icon: 'none'
        })
        this.Production = ''
        return
      }

    },
    closeTime() {
      this.showsc = false
    },
    //车辆详情
    selectCarDetail(val) {
      this.cardetail = val.value
      this.xqtitlet = val.label
      this.showxq = false
    },
    //手续
    getFormalties(val) {
      this.sxtitlet = val.label
      this.sxtitletid = val.value
      this.showsx = false
    },

    // 寻车发布数据提交
    async submitclick() {
      let submitData = {
        'store_Id': this.storeId,
        'branb_Name': this.specifAll.brandlist.brand,
        'car_Type_Id': this.specifAll.Serieslist.carTypeId,
        'car_Type_Name': this.specifAll.Serieslist.carTypeName,
        'car_Id': this.specifAll.Configulist.id,
        'car_Name': this.specifAll.Configulist.name,
        'price': this.specifAll.Configulist.factoryprice ? Number(this.specifAll.Configulist.factoryprice.split('万')[0]) *
					10000
          : null,
        'facade_PictureList': this.xedColorList,
        'goods_Region': this.goodsRegion,
        'goods_Region_Name': this.goodsRegionName,
        'region_Limit_Id': this.regionLimitId,
        'region_Limit_Name': this.regionLimitName,
        'deadline': this.Production,
        'procedur': this.sxtitletid,
        'car_Description': this.cardetail,
        'remark': this.remark
      }
      let showTxt = '请完善发布寻车信息'
      if (this.Whethertosubmit === '2') {
        submitData.specification = this.specifAll.Speciflist.id
        submitData.id = this.editId
        delete submitData.store_Id
        showTxt = '请完善编辑寻车信息'
      }

      let status = true
      for (let it in submitData) {
        // console.log(it, submitData[it])
        if (it === 'facade_PictureList') {
          if (submitData[it].length <= 0) {
            status = false
            uni.showToast({
              title: showTxt,
              duration: 2000,
              icon: 'none'
            })
            return
          }
        } else if (submitData[it] === '' || submitData[it] === null || submitData[it] === undefined) {
          if (it === 'remark') {} else {
            status = false
            uni.showToast({
              title: showTxt,
              duration: 2000,
              icon: 'none'
            })
            return
          }
        }
      }
      try {
        const res = this.Whethertosubmit === '1' ? await this.$api.findCar.AddCarGindB(submitData) : await this.$api.findCar
          .UpdateCarGindB((submitData))
        // console.log(res)
        this.open()
        uni.redirectTo({
          url: this.Whethertosubmit === '1' ? '/pages_common/releaseSuccess/releaseSuccess' : '/pages_findCar/iFoundTheCar/iFoundTheCar'
        })
        // uni.navigateTo({
        // })
      } catch (e) {
        //TODO handle the exception
      }

    },
    //发布成功弹框
    open() {
      uni.$off('specifAll')
      uni.$off('carSourceColor')
      uni.$off('selectCity')
    },
    // 获取规格
    async getSpeciflist() {
      try {
        const res = await this.$api.jsonData.specification()
        this.specificationList = res.data
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 取消寻车
    Inquiryonshelves() {

      let data = {
        'id': this.editId,
        'type': 0,
        'deadline': null
      }
      this.$api.findCar.UpdateState(data).then(res => {
        // console.log(res)
        uni.showToast({
          title: '已取消寻车！',
          duration: 2000,
          icon: 'success'
        })
        setTimeout(function() {
          uni.redirectTo({
            url: '/pages_findCar/iFoundTheCar/iFoundTheCar'
          })
        }, 2000)

      }).catch(err => {
        //    uni.showToast({
        // title: '已下架！',
        // duration: 2000,
        // icon: 'success'
        //    })
      })

    },
    // 获取编辑寻车详情
    async setEdit() {
      try {
        const res = await this.$api.findCar.GetCarGindBEchoData({
          id: this.editId
        })
        // console.log(res, this.xedColorList)
        this.storeId = res.data.store_Id,
        this.specifAll.brandlist.brand = res.data.branb_Name,
        this.specifAll.Serieslist.carTypeId = res.data.car_Type_Id,
        this.specifAll.Serieslist.carTypeName = res.data.car_Type_Name,
        this.specifAll.Configulist.id = res.data.car_Id,
        this.specifAll.Configulist.name = res.data.car_Name,
        this.specifAll.Configulist.factoryprice = res.data.price + '万'
        this.xedColorList = res.data.facade_PictureList,
        this.goodsRegion = res.data.goods_Region,
        this.goodsRegionName = res.data.goods_Region_Name,
        this.regionLimitId = res.data.region_Limit_Id,
        this.regionLimitName = res.data.region_Limit_Name,
        this.Production = res.data.deadline.split('T')[0],
        this.sxtitletid = res.data.procedur,
        this.cardetail = res.data.car_Description,
        this.remark = res.data.remark

        this.xedColorList.forEach(item => {
          if (item.inColour) {
            item.inColour.neiColor = item.inColour.colorCoding ? item.inColour.colorCoding.split('/') : []
          }
          if (item.outerColour) {
            item.outerColour.neiColor = item.outerColour.colorCoding ? item.outerColour.colorCoding.split('/') : []
          }
        })
        this.specificationList.forEach(it => {
          if (it.id === res.data.specification) {
            this.specifAll.Speciflist = it
          }
        })
      } catch (e) {
        //TODO handle the exception
      }
    },
  }
}
